package com.gxa.controller;

import com.gxa.common.JsonResult;
import com.gxa.common.PageRecv;
import com.gxa.common.R;
import com.gxa.pojo.Employee;
import com.gxa.pojo.Workreport;
import com.gxa.service.WorkReportSercvice;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;


@Controller
@Api(value = "工作报告数据",tags = "工作报告数据接口API-邹明伟")
@RequestMapping(value = "/WorkReport")
public class WorkReportController {
    @Autowired
    private WorkReportSercvice workReportSercvice;
//    查询工作报告
@RequestMapping(value = "/queryWorkRep",method = RequestMethod.GET)
@ResponseBody
    public JsonResult queryWorkRep(Integer currentPage,  Integer pageSize){
        return workReportSercvice.queryWorkRep(currentPage, pageSize);
    }

//    批注
    @RequestMapping(value = "/commentWorkRep",method = RequestMethod.POST)
@ResponseBody
    public R commentWorkRep(@RequestBody Workreport workreport){
        return workReportSercvice.commentWorkRep(workreport);
    }

//    删除
     @RequestMapping(value = "/deleteWorkRep",method = RequestMethod.POST)
@ResponseBody
     public R deleteWorkRep( Integer id){
return workReportSercvice.deleteWorkRep(id);
}

//    写报告（添加）
@RequestMapping(value = "/addWorkRep",method = RequestMethod.POST)
@ResponseBody
public R addWorkRep(Workreport workreport, HttpServletRequest request){
    R r=new R();
    if(workreport.getAppendix().isEmpty()){
        r.setCode(500);
        r.setMsg("没有选中文件");
    }else{
        String uploadPath = request.getServletContext().getRealPath("") + File.separator + "upload" + File.separator;
        File file = new File(uploadPath);
        if(!file.exists()){
            file.mkdirs();
        }
        MultipartFile multipartFile = workreport.getAppendix();
        String fileName = multipartFile.getOriginalFilename();
        File newFile = new File(uploadPath + fileName);
        System.out.println("newFile=" + newFile);
        try {
            //上传
            FileCopyUtils.copy(multipartFile.getBytes(),newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("文件名："+fileName);
        workreport.setAppendix_name(fileName);
    }

return workReportSercvice.addWorkRep(workreport);
}

//    选择接收人（查询员工）
@RequestMapping(value = "/queryEmp",method = RequestMethod.POST)
@ResponseBody
    public JsonResult queryEmpByDeptId(@RequestBody  Employee employee){
       return workReportSercvice.queryEmpByDeptId(employee);
    }

//    分类查询
    @RequestMapping(value = "/querySort")
    @ResponseBody
    public JsonResult searchquery(@RequestBody  PageRecv<Workreport> pageRecv){
        return workReportSercvice.searchquery(pageRecv);
    }

}
